//
//  KSFormVItem.swift
//  ANOA
//
//  Created by 邱仙凯 on 2022/5/23.
//  Copyright © 2025 aowang. All rights reserved.
//  上下格式的表单 单个数据源

import UIKit
let vItemTitleHight:CGFloat = 35
class KSFormVItem: KSFormBaseItem {
    
    override func disabledChange() {
        
        titleLabColorAdapt()
        textViewColorAdapt()
        if disabled {
           
       
            self.textView.placeHolder = ""
        } else {
 
            self.textView.placeHolder = self.appearance.placeholder
        }
 

       
       self.countV.isUserInteractionEnabled = !disabled
        self.segment.isUserInteractionEnabled = !disabled
        assistImgAdapt()
        layoutTextView()
    }
    // 改变字段的  填写或者选择  【searchFlag图标和内容控件联合布局】
    override func actionClosureChange() {
        assistImgAdapt()
        if actionClosure.isSome {
            if self.appearance.autoArrow {
                 self.layoutTextView()
            }
             self.bgAddTap()
         } else {
          
             self.layoutTextView()
         }
    }
 // // 【searchFlag图标和内容控件联合布局】
    override  func  tempApddTextView() {
   
       self.addSubview(textView)
        textView.font = self.appearance.contentFont

        textView.textColor = self.appearance.contentColor
        textView.backgroundColor = self.appearance.textBgColor
         textView.layer.borderWidth = 1
        textView.layer.cornerRadius = 4
        textView.layer.borderColor = UIColor(kshex: "eeeeee").cgColor
         textView.plaAlignment = .left
        textView.textAlignment = .left
          textView.delegate = self
        textView.textChanged = { [weak self] txt in
            guard let `self` = self else { return }
            
            if self.appearance.limitTextCount > 0 {
                if txt?.count ?? 0 > self.appearance.limitTextCount {
                    self.textView.text = self.textView.text!.subTo(self.appearance.limitTextCount)
                    
                    if self.appearance.overTextCountAlert {
                        KSToast("不能超过\(self.appearance.limitTextCount)字~")
                    }
                }
            }
        }
         layoutTextView()
   }
    //【searchFlag图标和内容控件联合布局】
    override func layoutTextView() {
        guard textView.superview.isSome else {return}
        textView.snp.remakeConstraints { make in
           make.right.equalTo(self).offset(-(self.appearance.arrowTrailing + 5))
           make.left.equalTo(self.tpTitleLab)
           make.top.equalTo(self.tpTitleLab.snp.bottom).offset(8)
           make.bottom.equalTo(self).offset(-6)
       }
    }
    override  func  addAssistImg() {
   
       self.addSubview(assistBtn)
        assistBtn.setBackgroundImage(UIImage(named:self.appearance.assistImgStr.unwrap), for: .normal)
      
            assistBtn.snp.makeConstraints { make in
                make.right.equalTo(self).offset(-self.appearance.arrowTrailing)
                make.width.equalTo(18)
                make.height.equalTo(18)
                make.centerY.equalTo(tpTitleLab)
            }
        
        assistBtn.addTouchUpInSideAction { _ in
            if let clo = self.assistBtnAction {
                clo()
            }
        }
   }
    override func layoutTitle() {
        tpTitleLab.snp.makeConstraints { make in
            make.left.equalTo(self).offset(widgetLeading)
            make.top.equalTo(self).offset(10)
        }
    }
//    override func layoutArrow() {
//    
//        arrowMark.snp.makeConstraints { make in
//                make.right.equalTo(self).offset(-self.appearance.arrowTrailing)
//                make.width.equalTo(18)
//                make.height.equalTo(18)
//                make.centerY.equalTo(textView)
//            }
//        
//        
//    }
}
